package problems;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import lib.MathLib;
import lib.Permutations;

public class Euler041 extends AbstractEuler {

	@Override
	public Number calculate() {
		List<String> digits = new LinkedList<String>(Arrays.asList("9", "8", "7", "6", "5", "4", "3", "2", "1"));

		do {
			Iterator<List<String>> p = new Permutations<String>(digits, digits.size());
			while (p.hasNext()) {
				List<String> nextBiggest = p.next();
				StringBuffer strbuf = new StringBuffer();
				for (String digit : nextBiggest) {
					strbuf.append(digit);
				}
				int asNumber = Integer.valueOf(strbuf.toString());
				if (MathLib.isPrime(asNumber)) return asNumber;
			}
			digits.remove(0);
		} while (digits.size() > 1);
		
		fail("no result found");
		return null;
	}

	@Override
	protected Number getCorrectAnswer() {
		return 7652413;
	}

}
